package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import database.DatabaseExtension;
import database.MyExpenditure;
import database.MyGood;
import database.MyShipment;
import database.NewTirDataBase;
import enums.DialogType;
import enums.SearchType;
import Other.Glob;
public class ShipmentItems extends JFrame {
	private JPanel contentPane;
	private JTextField tfCodeGoods;
	private JTextField tfDescriptionGoods;
	private JTextField tfMeasure;
	private JTextField tfUnits;
	private JTextField tfAmountGoods;
	private JTextField tfPackages;
	private JTextField tfCodeExpenditure;
	private JTextField tfDescriptionExpenditure;
	private JTextField tfTax;
	private JTextField tfAmountExpenditure;
	private DefaultTableModel northModel;
	private DefaultTableModel southModel;
	private JTable northTable;
	private JTable southTable;
	private JScrollPane northSP;
	private JFrame thisFrame = this;
	/**
	 * Create the frame.
	 */
	public ShipmentItems() {
		setTitle("Add Items to Shipment");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 747, 646);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.getInputMap(contentPane.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0), "");
		contentPane.getActionMap().put("", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				//request focus in the goods code
				resetFields();
			}
		});
		setContentPane(contentPane);
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		southTable = new JTable();
		JScrollPane southSP = new JScrollPane(southTable);
		JLabel lblFExpenditure = new JLabel("F2 Expenditure");
		tfCodeExpenditure = new JTextField();
		tfCodeExpenditure.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					try{
						//request focus for the next fields
						tfDescriptionExpenditure.requestFocus();
						if(!tfCodeExpenditure.getText().isEmpty()){
							enterDataExpenditure();
							}else{
								tfDescriptionExpenditure.requestFocus();
							}
					}catch (Exception e1){
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null, "Code must be a valid positive number and exist in the table");
						tfCodeExpenditure.setText("");
						tfCodeExpenditure.requestFocus();
						return;
					}
					System.out.println("button is being pressed");
				}
			}
		});
		tfCodeExpenditure.getInputMap(tfCodeExpenditure.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "");
		tfCodeExpenditure.getActionMap().put("", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				Glob.type = SearchType.EXPENDITURE;
				//remove item when F6 button pressed
				try {
					//NewTirDataBase c = (MyGood) DatabaseExtension.getTableID("SELECT * FROM good WHERE code = '" + tfCodeGoods.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
					NewTirDataBase c = (MyExpenditure) DatabaseExtension.getTableID("SELECT * FROM expenditure WHERE code = '" + tfCodeExpenditure.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
					DatabaseExtension.deleteExpenditure(((MyExpenditure) c).getCode(), ((MyExpenditure) c).getShipmentID());
					setSouthModel();
					resetFieldsExpenditure();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		tfCodeExpenditure.setColumns(10);
		tfDescriptionExpenditure = new JTextField();
		tfDescriptionExpenditure.getInputMap(contentPane.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "");
		tfDescriptionExpenditure.getActionMap().put("", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				//request focus in the goods code
				tfCodeExpenditure.requestFocus();
			}
		});
		tfDescriptionExpenditure.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					tfTax.requestFocus();
					Shipment s = Shipment.getInstance();
					String type;
					type = String.valueOf(DatabaseExtension.getLastColumnId("select tax.tax from tax, client where tax.taxid = client.taxid and client.clientid = '" + s.getClientCode() +"'"));
					if (type.equals("0")){
						tfTax.setText(null);
					}else{
						tfTax.setText(type);
						//select tax.type from tax, client where tax.taxid = client.taxid and client.clientid = '1';
					}
					System.out.println("button is being pressed");
				}
			}
		});
		tfDescriptionExpenditure.setColumns(10);
		JLabel label_1 = new JLabel("Code");
		JLabel label_2 = new JLabel("Description");
		JLabel lblTax = new JLabel("Tax");
		JLabel lblAmount_1 = new JLabel("Amount");
		tfTax = new JTextField();
		tfTax.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					tfAmountExpenditure.requestFocus();
					System.out.println("button is being pressed");
				}
			}
		});
		tfTax.setColumns(10);
		tfAmountExpenditure = new JTextField();
		tfAmountExpenditure.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					tfCodeExpenditure.requestFocus();
					try {
						saveDialogExpenditure();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					setSouthModel();
					//resetFieldsExpenditure();
					
					System.out.println("button is being pressed");
				}
			}
		});
		tfAmountExpenditure.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
				gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(southSP, GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
								.addComponent(lblFExpenditure)
								.addGroup(gl_panel_1.createSequentialGroup()
										.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_panel_1.createSequentialGroup()
														.addComponent(tfCodeExpenditure, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.UNRELATED)
														.addComponent(tfDescriptionExpenditure, GroupLayout.PREFERRED_SIZE, 394, GroupLayout.PREFERRED_SIZE))
														.addGroup(gl_panel_1.createSequentialGroup()
																.addComponent(label_1)
																.addGap(62)
																.addComponent(label_2)))
																.addGap(18)
																.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
																		.addComponent(lblTax)
																		.addComponent(tfTax, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(ComponentPlacement.UNRELATED)
																		.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
																				.addComponent(lblAmount_1)
																				.addComponent(tfAmountExpenditure, GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))))
																				.addContainerGap())
				);
		gl_panel_1.setVerticalGroup(
				gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblFExpenditure)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
								.addComponent(label_1)
								.addComponent(label_2)
								.addComponent(lblTax)
								.addComponent(lblAmount_1))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
										.addComponent(tfCodeExpenditure, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfDescriptionExpenditure, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfTax, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfAmountExpenditure, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(southSP, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
										.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		panel_1.setLayout(gl_panel_1);
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		JLabel lblFRemove = new JLabel("F6 Remove ");
		JLabel lblEscExit = new JLabel("Esc Exit");
		JLabel lblFReset = new JLabel("F7 Reset");
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
				gl_panel_2.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel_2.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblFRemove)
						.addPreferredGap(ComponentPlacement.RELATED, 388, Short.MAX_VALUE)
						.addComponent(lblEscExit)
						.addGap(43)
						.addComponent(lblFReset)
						.addGap(93))
				);
		gl_panel_2.setVerticalGroup(
				gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
						.addGap(33)
						.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblFRemove)
								.addComponent(lblEscExit)
								.addComponent(lblFReset))
								.addContainerGap(35, Short.MAX_VALUE))
				);
		panel_2.setLayout(gl_panel_2);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE)
				.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE)
				);
		gl_contentPane.setVerticalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE))
				);
		JLabel lblFGoods = new JLabel("F1 Goods");
		JLabel lblCode = new JLabel("Code");
		tfCodeGoods = new JTextField();
		tfCodeGoods.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					try{
						//request focus for the next fields
						tfDescriptionGoods.requestFocus();
						if(!tfCodeGoods.getText().isEmpty()){
						enterDataGoods();
						}else{
							tfDescriptionGoods.requestFocus();
						}
					}catch (Exception e1){
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null, "Code must be a valid positive number and exist in the table");
						tfCodeGoods.setText("");
						tfCodeGoods.requestFocus();
						return;
					}
					System.out.println("button is being pressed");
				}
			}
		});
		tfCodeGoods.getInputMap(tfCodeGoods.WHEN_FOCUSED)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "");
		tfCodeGoods.getActionMap().put("", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				Glob.type = SearchType.GOOD;
				//remove item when F6 button pressed
				try {
					//NewTirDataBase c = (MyGood) DatabaseExtension.getTableID("SELECT * FROM good WHERE code = '" + tfCodeGoods.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
					NewTirDataBase c = (MyGood) DatabaseExtension.getTableID("SELECT * FROM good WHERE code = '" + tfCodeGoods.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
					DatabaseExtension.deleteGood(((MyGood) c).getCode(), ((MyGood) c).getShipmentID());
					setNorthModel();
					resetFieldsGood();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		tfCodeGoods.setColumns(10);
		JLabel lblDescription = new JLabel("Description");
		tfDescriptionGoods = new JTextField();
		tfDescriptionGoods.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfMeasure.requestFocus();
					System.out.println("button is being pressed");
				}
			}
		});
		tfDescriptionGoods.getInputMap(tfCodeGoods.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
		tfDescriptionGoods.getActionMap().put("", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				escape();
			}
		});
		tfDescriptionGoods.setColumns(10);
		JLabel lblMeasure = new JLabel("Measure");
		tfMeasure = new JTextField();
		tfMeasure.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfUnits.requestFocus();
					System.out.println("button is being pressed");
				}
			}
		});
		tfMeasure.getInputMap(contentPane.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "");
		tfMeasure.getActionMap().put("", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				//request focus in the goods code
				tfCodeGoods.requestFocus();
			}
		});
		tfMeasure.setColumns(10);
		JLabel lblUnits = new JLabel("Units");
		tfUnits = new JTextField();
		tfUnits.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfAmountGoods.requestFocus();
					System.out.println("button is being pressed");
				}
			}
		});
		tfUnits.setColumns(10);
		JLabel lblAmount = new JLabel("Amount");
		tfAmountGoods = new JTextField();
		tfAmountGoods.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfPackages.requestFocus();
					System.out.println("button is being pressed");
				}
			}
		});
		tfAmountGoods.setColumns(10);
		JLabel lblPackages = new JLabel("Packages");
		tfPackages = new JTextField();
		tfPackages.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a type is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					//request focus for the next fields
					tfCodeGoods.requestFocus();
					try {
						saveDialogGood();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					setNorthModel();
					//resetFieldsGood();
					System.out.println("button is being pressed");
				}
			}
		});
		tfPackages.setColumns(10);
		northTable = new  JTable(northModel);
		northSP = new JScrollPane(northTable);
		//northArea = new JTextArea();
		// northSP = new JScrollPane(northArea);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(northSP, GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
								.addComponent(lblFGoods)
								.addGroup(gl_panel.createSequentialGroup()
										.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_panel.createSequentialGroup()
														.addComponent(tfCodeGoods, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.UNRELATED)
														.addComponent(tfDescriptionGoods, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.UNRELATED)
														.addComponent(tfMeasure, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.UNRELATED)
														.addComponent(tfUnits, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
														.addGroup(gl_panel.createSequentialGroup()
																.addComponent(lblCode)
																.addGap(62)
																.addComponent(lblDescription)
																.addGap(216)
																.addComponent(lblMeasure)
																.addGap(48)
																.addComponent(lblUnits)))
																.addPreferredGap(ComponentPlacement.UNRELATED)
																.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
																		.addComponent(tfAmountGoods, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
																		.addComponent(lblAmount))
																		.addPreferredGap(ComponentPlacement.UNRELATED)
																		.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
																				.addGroup(gl_panel.createSequentialGroup()
																						.addComponent(lblPackages)
																						.addGap(16))
																						.addComponent(tfPackages, GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE))))
																						.addContainerGap())
				);
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblFGoods)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblCode)
								.addComponent(lblDescription)
								.addComponent(lblMeasure)
								.addComponent(lblUnits)
								.addComponent(lblAmount)
								.addComponent(lblPackages))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
										.addComponent(tfCodeGoods, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfDescriptionGoods, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfMeasure, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfUnits, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfAmountGoods, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfPackages, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(northSP, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
										.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
	public void setNorthModel(){
		Shipment s = Shipment.getInstance();
		northModel = buildTableModel("select code, description, measure, unit, amount, parcel from good where shipmentID = '" + s.getFieldCode() +"'");
		System.out.println("This is the model size " + northModel.getColumnCount());
		northTable.setModel(northModel);
		//DatabaseExtension.getTableID("SELECT * FROM Good WHERE ShipmentID = '" + s.getFieldCode() +"'");
		//DatabaseExtension.printQueryArea("SELECT * FROM Good WHERE ShipmentID = '" + s.getFieldCode() +"'", northArea);
	}
	public void setSouthModel(){
		Shipment s = Shipment.getInstance();
		southModel = buildTableModel("select code, description, tax, amount from expenditure where shipmentID = '" + s.getFieldCode() +"'");
		System.out.println("This is the model size " + northModel.getColumnCount());
		southTable.setModel(southModel);
	}
	public  DefaultTableModel buildTableModel(String query) {
		//variable that holds the ids of the 
		ArrayList<Integer> ids;
		ids = new ArrayList<Integer>();
		Vector<String> columnNames = null;
		Vector<Vector<Object>> data = null;
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			//connect with the database
			Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/newtir", "root", "");
			// The Connection is obtained
			Statement stmt = (Statement) con.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			// System.out.println("this is the string got from search Bar " + s);
			ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();
			// names of columns
			columnNames = new Vector<String>();
			int columnCount = metaData.getColumnCount();
			for (int column = 1; column <= columnCount; column++) {
				columnNames.add(metaData.getColumnName(column));
			}
			// data of the table
			data = new Vector<Vector<Object>>();
			while (rs.next()) {
				Vector<Object> vector = new Vector<Object>();
				for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
					Object ss  =rs.getObject(columnIndex);
					vector.add(ss);
					//System.out.println("Iterating");
					if (columnIndex==1){
						//System.out.println("This is the buidlTAblemethod adding column indexes to an array" + ss);
						ids.add(Integer.valueOf(ss.toString()));
					}
				}
				data.add(vector);
			}
			rs.close();
			stmt.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DefaultTableModel(data, columnNames);
	}
	public void escape(){
		thisFrame.dispose();
		Shipment.getInstance().enableShipment();
	}
	public void saveDialogGood() throws Exception{
		if (tfDescriptionGoods.getText().isEmpty()){
			
			JOptionPane.showMessageDialog(null, "Description cannot be empty");
			tfDescriptionGoods.requestFocus();
			return;
		}
		try{	
			if(!tfUnits.getText().isEmpty()) {
				if(Float.valueOf(tfUnits.getText())<=-1){
					JOptionPane.showMessageDialog(null, "Units has to be a positive number");
					tfUnits.setText("");
					tfUnits.requestFocus();
					return;
				}
			}
		}catch (NumberFormatException type){
			type.printStackTrace();
			JOptionPane.showMessageDialog(null, "Units has to be a positive number");
			tfUnits.setText(null);
			tfUnits.requestFocus();
			return;
		}
		try{	
			if(!tfAmountGoods.getText().isEmpty()) {
				if(Float.valueOf(tfAmountGoods.getText())<=-1){
					JOptionPane.showMessageDialog(null, "Amount has to be a positive number with two decimals the most");
					tfAmountGoods.setText("");
					tfAmountGoods.requestFocus();
					return;
				}
			}
		}catch (NumberFormatException type){
			type.printStackTrace();
			JOptionPane.showMessageDialog(null, "Amount has to be a positive number with two decimals the most");
			tfAmountGoods.setText(null);
			tfAmountGoods.requestFocus();
			return;
		}
		try{	
			if(tfPackages.getText().trim().length()>1) {
				if(Float.valueOf(tfPackages.getText())<=-1){
					JOptionPane.showMessageDialog(null, "Package has to be a positive number");
					tfPackages.setText("");
					tfPackages.requestFocus();
					return;
				}
			}
		}catch (NumberFormatException type){
			type.printStackTrace();
			JOptionPane.showMessageDialog(null, "Package has to be a positive number");
			tfPackages.setText(null);
			tfPackages.requestFocus();
			return;
		}
		saveGood();
	}
	public void saveGood(){
		Glob.type = SearchType.GOOD;
		String[] data = new String[8];	
		data[0] = null; 
		try{
			data[1] = tfCodeGoods.getText();	
			if(data[1].isEmpty()){
				Shipment s = Shipment.getInstance();
				data[1] = String.valueOf(DatabaseExtension.getLastColumnId("select code from good where shipmentID = '" + s.getFieldCode() +"'") + 1);
				System.out.println("This is the last ID from code in goods " + data[1]);	
			}else{
				data[1] = tfCodeGoods.getText();	
			}
		}catch(Exception code){
			code.printStackTrace();
		}
		data[2] = tfDescriptionGoods.getText();
		data[3] = tfMeasure.getText();
		if(tfUnits.getText().isEmpty()){
			data[4] = "0";
		}else{
			data[4] = tfUnits.getText();
		}
		if(tfAmountGoods.getText().isEmpty()){
			data[5] = "0";
		}else{
			data[5] = tfAmountGoods.getText();
		}
		if(tfPackages.getText().isEmpty()){
			data[6] = "0";
		}else{
			data[6] = tfPackages.getText();
		}
		data[7] = Shipment.getInstance().getFieldCode();
		MyGood good = new MyGood(data);
		try {
			String shipmentID = Shipment.getInstance().getFieldCode();
			DatabaseExtension.saveGood(good, shipmentID);
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("This is the exception released in saving good");
			JOptionPane.showMessageDialog(null, "Exception when saving goods");
			tfCodeGoods.requestFocus();
			return;
		}
		resetFieldsGood();
	}
	public void enterDataGoods() throws Exception{
		//set up global variable for the Tax screen
		Glob.type = SearchType.GOOD;
		try{
			if(tfCodeGoods.getText().isEmpty()){
				tfDescriptionGoods.requestFocus();
			}
			else if(Integer.parseInt(tfCodeGoods.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCodeGoods.setText("");
				tfCodeGoods.requestFocus();
				return;
			}
		}catch (NumberFormatException number){
			JOptionPane.showMessageDialog(null, "Code must be a positive number");
			tfCodeGoods.setText("");
			tfCodeGoods.requestFocus();
			return;
		}
		NewTirDataBase c = (MyGood) DatabaseExtension.getTableID("SELECT * FROM good WHERE code = '" + tfCodeGoods.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
		//NewTirDataBase c = (MyGood) DatabaseExtension.getTableID("SELECT * FROM good WHERE code = '" + tfCodeGoods.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
		tfDescriptionGoods.setText(((MyGood) c).getDescription());
		tfMeasure.setText(((MyGood) c).getMeasure());
		tfUnits.setText(((MyGood) c).getUnit());
		tfAmountGoods.setText(((MyGood) c).getAmount());
		tfPackages.setText(((MyGood) c).getParcel());
	}
	public void enterDataExpenditure() throws Exception{
		//set up global variable for the Tax screen
		Glob.type = SearchType.EXPENDITURE;
		try{
			if(tfCodeExpenditure.getText().isEmpty()){
				tfDescriptionExpenditure.requestFocus();
			}
			else if(Integer.parseInt(tfCodeExpenditure.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCodeExpenditure.setText("");
				tfCodeExpenditure.requestFocus();
				return;
			}
		}catch (NumberFormatException number){
			JOptionPane.showMessageDialog(null, "Code must be a positive number");
			tfCodeExpenditure.setText("");
			tfCodeExpenditure.requestFocus();
			return;
		}
		NewTirDataBase c = (MyExpenditure) DatabaseExtension.getTableID("SELECT * FROM expenditure WHERE code = '" + tfCodeExpenditure.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
		//NewTirDataBase c = (MyGood) DatabaseExtension.getTableID("SELECT * FROM good WHERE code = '" + tfCodeGoods.getText() +"' and ShipmentID = '"+ Shipment.getInstance().getFieldCode() +"'")[0];
		tfDescriptionExpenditure.setText(((MyExpenditure) c).getDescription());
		tfTax.setText(((MyExpenditure) c).getTax());
		tfAmountExpenditure.setText(((MyExpenditure) c).getAmount());
	}
	public void resetFields(){
		tfCodeGoods.setText(null);
		tfDescriptionGoods.setText(null);
		tfMeasure.setText(null);
		tfUnits.setText(null);
		tfAmountGoods.setText(null);
		tfPackages.setText(null);
		tfCodeExpenditure.setText(null);
		tfDescriptionExpenditure.setText(null);
		tfTax.setText(null);;
		tfAmountExpenditure.setText(null);
	}
	public void resetFieldsGood(){
		tfCodeGoods.setText(null);
		tfDescriptionGoods.setText(null);
		tfMeasure.setText(null);
		tfUnits.setText(null);
		tfAmountGoods.setText(null);
		tfPackages.setText(null);
	}
	public void resetFieldsExpenditure(){
		tfCodeExpenditure.setText(null);
		tfDescriptionExpenditure.setText(null);
		tfTax.setText(null);;
		tfAmountExpenditure.setText(null);
	}
	public void saveDialogExpenditure() throws Exception{
		if (tfDescriptionExpenditure.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "Description cannot be empty");
			tfDescriptionExpenditure.requestFocus();
			return;
		}
		try{	
			if(!tfTax.getText().isEmpty()) {
				if(Float.valueOf(tfTax.getText())<=-1){
					JOptionPane.showMessageDialog(null, "Tax has to be a positive decimal number");
					tfTax.setText("");
					tfTax.requestFocus();
					return;
				}
			}
		}catch (NumberFormatException type){
			type.printStackTrace();
			JOptionPane.showMessageDialog(null, "Tax has to be a positive decimal number");
			tfTax.setText(null);
			tfTax.requestFocus();
			return;
		}
		try{	
			if(!tfAmountExpenditure.getText().isEmpty()) {
				if(Float.valueOf(tfAmountExpenditure.getText())<=-1){
					JOptionPane.showMessageDialog(null, "Amount has to be a positive number with two decimals the most");
					tfAmountExpenditure.setText("");
					tfAmountExpenditure.requestFocus();
					return;
				}
			}
		}catch (NumberFormatException type){
			type.printStackTrace();
			JOptionPane.showMessageDialog(null, "Amount has to be a positive number with two decimals the most");
			tfAmountExpenditure.setText(null);
			tfAmountExpenditure.requestFocus();
			return;
		}
		saveExpenditure();
	}
	public void saveExpenditure(){
		Glob.type = SearchType.EXPENDITURE;
		String[] data = new String[8];	
		data[0] = null; 
		try{
			data[1] = tfCodeExpenditure.getText();	
			if(data[1].isEmpty()){
				Shipment s = Shipment.getInstance();
				data[1] = String.valueOf(DatabaseExtension.getLastColumnId("select code from expenditure where shipmentID = '" + s.getFieldCode() +"'") + 1);
				System.out.println("This is the last ID from code in expenditure " + data[1]);	
			}else{
				data[1] = tfCodeExpenditure.getText();	
			}
		}catch(Exception code){
			code.printStackTrace();
		}
		data[2] = tfDescriptionExpenditure.getText();
		if(tfTax.getText().isEmpty()){
			data[3] = "0";
		}else{
			data[3] = tfTax.getText();
		}
		if(tfAmountExpenditure.getText().isEmpty()){
			data[4] = "0";
		}else{
			data[4] = tfAmountExpenditure.getText();
		}
		data[5] = Shipment.getInstance().getFieldCode();
		MyExpenditure expenditure = new MyExpenditure(data);
		try {
			String shipmentID = Shipment.getInstance().getFieldCode();
			DatabaseExtension.saveExpenditure(expenditure, shipmentID);
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("This is the exception released in saving expenditure");
			JOptionPane.showMessageDialog(null, "Exception when saving expenditure");
			tfCodeGoods.requestFocus();
			return;
		}
		resetFieldsExpenditure();
	}
}
